package No0224_basic_calculator

func calculate(s string) int {
	var res int = 0
	var l = len(s)
	var nums = make([]int, 0)
	var opts = make([]int, 0)

	var sign = 1
	for i := 0; i < l; i++ {
		if s[i] >= '0' && s[i] <= '9' { //是数字
			var tmpNum int = int(s[i]) - int('0')
			for i < l-1 && s[i+1] >= '0' && s[i+1] <= '9' {
				tmpNum = tmpNum*10 + (int(s[i+1]) - int('0'))
				i++
			}
			res += sign * tmpNum
		} else if s[i] == '+' {
			sign = 1
		} else if s[i] == '-' {
			sign = -1
		} else if s[i] == '(' {
			//左括号 将结果暂存
			nums = append(nums, res)
			opts = append(opts, sign)
			sign = 1
			res = 0
		} else if s[i] == ')' {
			//右括号 取出栈中暂存的结果并更新
			res = nums[len(nums)-1] + res*opts[len(opts)-1]
			nums = nums[:len(nums)-1]
			opts = opts[:len(opts)-1]
		}
	}
	return res
}

var Solution func(string) int = calculate
